$(function(){
	
	
	function getTableSource(){
		var status = $("#status").val();
		var tableSource = new $.jqx.dataAdapter({
			dataType: "json",
			dataFields:[
			    { name: "id", type: "int" },
			    { name: "uuid", type: "string" },
			    { name: "name", type: "string" },
			    { name: "contacts", type: "string" },
			    { name: "telephone", type: "string" },
			    { name: "cellphone", type: "string" },
			    { name: "idcard", type: "string" },
				{ name: "idcard2", type: "string" },
			    { name: "email", type: "string" },
		  	    { name: "status", type: "int" },
			    { name: "province", type: "string" },
			    { name: "city", type: "string" },
			    { name: "district", type: "string" },
			    { name: "street", type: "string" },
			    { name: "detailed_address", type: "string" },
			    { name: "description", type: "string" },
			    { name: "idcard_number", type: "string" },
			    { name: "business_licence", type: "string" },
				{ name: "tax_permit", type: "string" },
				{ name: "organization_permit", type: "string" },
				{ name: "bank_permit", type: "string" },
			    { name: "account_holder", type: "string" },
			    { name: "alipay", type: "string" },
			    { name: "status", type: "int" },
			    { name: "logo", type: "string" },
			    { name: "isBZJ",type:"int"},
			    { name: "sellerid", type:"string" },
			    { name: "settingid", type: "string"},
			    { name: "xn_account", type: "string"},
			],
			id: "id",
			url: "shop/all.do"
		});
		//alert(JSON.stringify(tableSource));
		return tableSource;
	}
	
	$("#dataTable").jqxDataTable({
		width: "100%",
        source: getTableSource(),
        pageable: true,
        serverProcessing: true,
        pageSize: 18,
        pagerButtonsCount: 10,
        columnsResize: true,
        sortable: true,
        columns: [
            { text: "序号", width: 50, cellsRenderer: function(row) { return (row + 1).toString(); } },
            { text: "店铺名称", dataField: "name", width: 200 },
            { text: "联系人", dataField: "contacts", width: 120 },
            { text: "联系电话", dataField: "telephone", width: 200, 
            	cellsRenderer: function(row, column, value, rowData) {
            		return value ? rowData.cellphone ? value + "/" + rowData.cellphone : value : rowData.cellphone ? rowData.cellphone:"无";
            	} 
            },
            { text: "邮箱", dataField: "email", width: 150 },
            { text: "支付宝账户", dataField: "alipay", width: 200},
            { text: "地区", dataField: "district", width: 200, 
            	cellsRenderer: function(row, column, value, rowData) {
            		return rowData.province + "." + rowData.city + "." + rowData.district;
            	}
            },
            { text: "街道", dataField: "street", width: 120 },
            { text: "详细地址", dataField: "detailed_address", width: 200 },
            { text: "简要说明", dataField: "description", width: 200 },
            { dataField: "logo", hidden: true },
            { dataField: "business_licence", hidden: true },
            { dataField: "idcard_number", hidden: true },
            { text:"是否缴纳保证金",dataField: "isBZJ",width:200,
            	cellsRenderer:function(row,column,value,rowData){
            		//alert(JSON.stringify(rowData));
            		//alert(value);
            		if(rowData.isBZJ == "0"){
            			return "尚未缴纳保证金";
            		}else{
            			return "已缴纳保证金";
            		}
            	}
            }
        ],
	});
	$("#dataTable").on("rowDoubleClick",function(info, args){
		if(args && args.flag){
			info = args;
		}
		editingIndex = info.args.index;
		editingData = info.args.row;
	 	_editerIndex = layer.open({
			id: "sellerEditer",
			title: "查看/修改店铺信息",
		    type: 1,
		    skin: 'layui-layer-rim',
		    area: ['1000px', '640px'],
		    content: 
		    '<form id="seller-editer" class="form" accept-charset="utf-8"  method="post" style="height: 100%;overflow: auto;">' +
				'<input type="hidden" name="id" id="shopId" />' +
				'<input type="hidden" id="province" name="province" />' +
				'<input type="hidden" id="city" name="city" />' +
				'<input type="hidden" id="district" name="district" />' +
				'<input type="hidden" name="submit-url" value="shop/update.do" />' +
				'<label class="from-label" for="name">店铺名称：</label>' +
				'<input type="text" name="name" allowBlank="false" class="form-control" placeholder="店铺名称" />' +
				'<label class="from-label" for="contacts">联系人：</label>' +
				'<input type="text" name="contacts" allowBlank="false" class="form-control" placeholder="联系人" />' +
				'<label class="from-label" for="telephone">座机号码：</label>' +
				'<input type="text" name="telephone" allowBlank="false" class="form-control" placeholder="座机号码" />' +
				'<label class="from-label" for="cellphone">手机号码：</label>' +
				'<input type="text" name="cellphone" allowBlank="false" class="form-control" placeholder="手机号码" />' +
				'<label class="from-label" for="idcard_number">身份证号：</label>' +
				'<input type="text" name="idcard_number" allowBlank="false" class="form-control" placeholder="身份证号" />' +
				'<label class="from-label" for="email">E-mail：</label>' +
				'<input type="text" name="email" allowBlank="false" class="form-control" placeholder="E-mail" />' +
				'<label class="from-label" for="address">地区：</label>' +
				'<span id="address" name="address" placeholder="地址" class="form-control" style="padding:0;"><span id="store-selector"></span></span>' +
				'<label class="from-label" for="street">街道：</label>' +
				'<input type="text" name="street" class="form-control" placeholder="街道" />' +
				'<label class="from-label" for="detailed_address">详细地址：</label>' +
				'<input type="text" name="detailed_address" allowBlank="false" class="form-control" placeholder="详细地址" />' +
				//添加是否缴费
				'<label class="from-label" for="isBZJ">保证金：</label>' +
				'<select id = "isBZJ" class="form-control" name = "isBZJ" allowBlank="false"><option value ="0">尚未缴纳</option><option value ="1">已缴纳</option></select>'+
				// 小能客服商户登录账号
				'<label class="from-label" for="xn_account">客服商户登录账号：</label>' +
				'<input type="text" name="xn_account" id="xn_account" allowBlank="true" class="form-control" placeholder="客服商户登录账号" />' +
				// 小能客服商户ID
				'<label class="from-label" for="sellerid">客服商户ID：</label>' +
				'<input type="text" name="sellerid"  id="sellerid" allowBlank="true" class="form-control" placeholder="客服商户ID" />' +
				// 小能客服客服组ID
				'<label class="from-label" for="settingid">客服接待组ID：</label>' +
				'<input type="text" name="settingid" id="settingid" allowBlank="true" class="form-control" placeholder="小能客服商户ID" />' +
				
				//占位符
				'<label class="from-label" style="width:100%" for="detailed_address"></label>' +
				
				'<label class="from-label" for="description">摘要概述：</label>' +
				'' +	'<label style="    visibility: hidden;" class="from-label" for="business_licence">身份证正面：</label>' +
				'<div class="fileupload fileupload-new data-provides" style="display:inline-block;vertical-align: middle;">' +
					'<div class="form-control fileupload-new thumbnail" style="width:830px;height:400px;padding:0;line-height:400px;">' +
						'<img name="idcard" allowBlank="true" placeholder="身份证正面">' +
					'</div>' +
					'<div class="form-control fileupload-preview fileupload-exists thumbnail" style="width:830px;height:400px;padding:0;"></div>' +
					'<div id="uploadimg">' +
						'<input type="hidden" name="idcard"/>' +
						'<input type="file" name="aaaaaaaa" id="idcard" style="display:none;" onchange="fileUpload(this)" />' +
						'<label for="idcard" class="fileupload-new btn btn-sm btn-success" style="margin:0">选择文件</label>' +
						'<label for="idcard" class="fileupload-exists btn btn-sm btn-success" style="margin:0">更改</label>' +
						'<span class="fileupload-exists data-dismiss btn btn-sm btn-danger">移除</span>' +
					'</div>' +
				'</div>' +
				'<label class="from-label" for="idcard">身份证正面：</label>' +
				'<div class="fileupload fileupload-new data-provides" style="display:inline-block;vertical-align: middle;">' +
					'<div class="form-control fileupload-new thumbnail" style="width:830px;height:400px;padding:0;line-height:400px;">' +
						'<img name="idcard2" allowBlank="true" placeholder=“身份证反面"/>' +
					'</div>' +
					'<div class="form-control fileupload-preview fileupload-exists thumbnail" style="width:830px;height:400px;padding:0;"></div>' +
					'<div id="uploadimg">' +
						'<input type="hidden" name="idcard2"/>' +
						'<input type="file" name="aaaaaaaa" id="idcard2" style="display:none;" onchange="fileUpload(this)" />' +
						'<label for="idcard2" class="fileupload-new btn btn-sm btn-success" style="margin:0">选择文件</label>' +
						'<label for="idcard2" class="fileupload-exists btn btn-sm btn-success" style="margin:0">更改</label>' +
						'<span class="fileupload-exists data-dismiss btn btn-sm btn-danger">移除</span>' +
					'</div>' +
				'</div>' +
				'<label class="from-label" for="idcard2">身份证反面：</label>' +
				'<div class="fileupload fileupload-new data-provides" style="display:inline-block;vertical-align: middle;">' +
					'<div class="form-control fileupload-new thumbnail" style="width:830px;height:400px;padding:0;line-height:400px;">' +
						'<img name="business_licence" allowBlank="true" placeholder="营业执照"/>' +
					'</div>' +
					'<div class="form-control fileupload-preview fileupload-exists thumbnail" style="width:830px;height:400px;padding:0;"></div>' +
					'<div id="uploadimg">' +
						'<input type="hidden" name="business_licence"/>' +
						'<input type="file" name="aaaaaaaa" id="business_licence" style="display:none;" onchange="fileUpload(this)" />' +
						'<label for="business_licence" class="fileupload-new btn btn-sm btn-success" style="margin:0">选择文件</label>' +
						'<label for="business_licence" class="fileupload-exists btn btn-sm btn-success" style="margin:0">更改</label>' +
						'<span class="fileupload-exists data-dismiss btn btn-sm btn-danger">移除</span>' +
					'</div>' +
				'</div>' +
				'<label class="from-label" for="business_licence">营业执照：</label>' +
				'<div class="fileupload fileupload-new data-provides" style="display:inline-block;vertical-align: middle;">' +
					'<div class="form-control fileupload-new thumbnail" style="width:830px;height:400px;padding:0;line-height:400px;">' +
						'<img name="tax_permit" allowBlank="true" placeholder="税务登记证"/>' +
					'</div>' +
					'<div class="form-control fileupload-preview fileupload-exists thumbnail" style="width:830px;height:400px;padding:0;"></div>' +
					'<div id="uploadimg">' +
						'<input type="hidden" name="tax_permit"/>' +
						'<input type="file" name="aaaaaaaa" id="tax_permit" style="display:none;" onchange="fileUpload(this)" />' +
						'<label for="tax_permit" class="fileupload-new btn btn-sm btn-success" style="margin:0">选择文件</label>' +
						'<label for="tax_permit" class="fileupload-exists btn btn-sm btn-success" style="margin:0">更改</label>' +
						'<span class="fileupload-exists data-dismiss btn btn-sm btn-danger">移除</span>' +
					'</div>' +
				'</div>' +
				'<label class="from-label" for="tax_permit">税务登记证：</label>' +
				'<div class="fileupload fileupload-new data-provides" style="display:inline-block;vertical-align: middle;">' +
					'<div class="form-control fileupload-new thumbnail" style="width:830px;height:400px;padding:0;line-height:400px;">' +
						'<img name="organization_permit" allowBlank="true" placeholder="组织机构代码证"/>' +
					'</div>' +
					'<div class="form-control fileupload-preview fileupload-exists thumbnail" style="width:830px;height:400px;padding:0;"></div>' +
					'<div id="uploadimg">' +
						'<input type="hidden" name="organization_permit" />' +
						'<input type="file" name="aaaaaaaaorganization_permit" id="organization_permit" style="display:none;" onchange="fileUpload(this)" />' +
						'<label for="organization_permit" class="fileupload-new btn btn-sm btn-success" style="margin:0">选择文件</label>' +
						'<label for="organization_permit" class="fileupload-exists btn btn-sm btn-success" style="margin:0">更改</label>' +
						'<span class="fileupload-exists data-dismiss btn btn-sm btn-danger">移除</span>' +
						'</div>' +
				'</div>' +
				'<label class="from-label" for="organization_permit">组织机构代码证：</label>' +
				'<div class="fileupload fileupload-new data-provides" style="display:inline-block;vertical-align: middle;">' +
					'<div class="form-control fileupload-new thumbnail" style="width:830px;height:400px;padding:0;line-height:400px;">' +
						'<img name="bank_permit" allowBlank="true" placeholder="银行开户许可证"/>' +
					'</div>' +
					'<div class="form-control fileupload-preview fileupload-exists thumbnail" style="width:830px;height:400px;padding:0;"></div>' +
					'<div id="uploadimg">' +
						'<input type="hidden" name="bank_permit" />' +
						'<input type="file" name="aaaabank_permit" id="bank_permit" style="display:none;" onchange="fileUpload(this)"/>' +
						'<label for="bank_permit" class="fileupload-new btn btn-sm btn-success" style="margin:0">选择文件</label>' +
						'<label for="bank_permit" class="fileupload-exists btn btn-sm btn-success" style="margin:0">更改</label>' +
						'<span class="fileupload-exists data-dismiss btn btn-sm btn-danger">移除</span>' +
						'</div>' +
				'</div>' +
				'<label class="from-label" for="bank_permit">银行开户许可证：</label>' +
				'<div class="fileupload fileupload-new data-provides" style="display:inline-block;vertical-align: middle;">' +
				'<div class="form-control fileupload-new thumbnail" style="width:830px;height:400px;padding:0;line-height:400px;">' +
					'<img name="logo" allowBlank="true" placeholder="LOGO"/>' +
				'</div>' +
				'<div class="form-control fileupload-preview fileupload-exists thumbnail" style="width:830px;height:400px;padding:0;"></div>' +
				'<div id="uploadimg">' +
					'<input type="hidden" name="logo"/>' +
					'<input type="file" name="aaaalogo" id="logo" style="display:none;" onchange="fileUpload(this)" />' +
					'<label for="logo" class="fileupload-new btn btn-sm btn-success" style="margin:0">选择文件</label>' +
					'<label for="logo" class="fileupload-exists btn btn-sm btn-success" style="margin:0">更改</label>' +
					'<span class="fileupload-exists data-dismiss btn btn-sm btn-danger">移除</span>' +
					'</div>' +
			'</div>' +
			'<label class="from-label" for="logo">LOGO：</label>' +
				'<br><br><br>' +
			'</form>' +
			'<div class="form" style="position: absolute;bottom: 0px;width: 100%;text-align: right;background-color: #DDD;">' +
				'<input type="button" class="btn btn-sm btn-success" onclick="submit(this)" value="保存"/>' +
				'<input type="button" class="btn btn-sm btn-info" onclick="closeEditer(this)" value="返回"/>' +
			'</div>',
		});
	 	
	 	$("input[name='id']").val(editingData['id']);
	 	$("#address").treemenu({ jsondata: treedata, deep: 2 });
	 	$("#address").treemenu("setText", editingData.province + "." + editingData.city + "." + editingData.district);
	 	$("#isBZJ option[text="+editingData.isBZJ+"]").attr("selected", true);
	 /*	obj = document.getElementById("IsBZJ");
	 	for(i=0;i<obj.length;i++){
	 		if(obj[i].value == editingData.isBZJ==1)
	 		{
	 			obj[i].selected = true;
	 		}
	 	}*/
	 	setData($("#seller-editer"), editingData);
	});
	
	
	//状态选择
	$("#statusUl li").click(function(){
		if($(this).attr("id")==="1"){
			$("#status").val(1);
		}else{
			$("#status").val(2);
		}
	});
	
	$("#select").click(function(){
		var status = $("#status").val();
		if(status === "1"){
			$("#disable").show();
			$("#enable").hide();
		}else{
			$("#disable").hide();
			$("#enable").show();
		}
	});
	
	//禁用
	$("#disable").click(function(){
		if(rows = isSelected()){
			setStatus({
				id:rows[0].id,
				status:2
			});
		}
	});
	
	//启用
	$("#enable").click(function(){
		if(rows = isSelected()){
			setStatus({
				id:rows[0].id,
				status:1
			});
		}
	});
	
	//更改商店状态
	function setStatus(p){
		$.get("shop/setStatus.do",p,function(data){
			layer.msg(data.msg);
			if(data.code==1){
				for (i = rows.length - 1; i >= 0; i--) {
					var row = rows[i];
					if (row.id === p.id) {
						$("#dataTable").jqxDataTable('deleteRow', i);
					}
				}
			}
		});
	}
});

function submit(obj){
	
	var text = $("#address").treemenu("getText");
	
	$("#province").val(text.split(".")[0]);
	$("#city").val(text.split(".")[1]);
	$("#district").val(text.split(".")[2]);
	document.charset="utf-8";
	submitEditer(obj);
}



//不知干嘛用的
function doSubmit(form, data, url) {
	var address = data.address;
	if(address) {
		var args = address.split(".");
		data.province = args[0];
		data.city = args[1];
		data.district = args[2];
	}
}


/**
 * 是否选中一项
 * 
 * @returns
 */
function isSelected() {
	var rows = $("#dataTable").jqxDataTable("getSelection");
	if (rows.length < 1) {
		layer.msg("请选择一行数据", {
			shift : 6,
			offset : "5%"
		});
		return null;
	}
	return rows;
}


function fileUpload(_this){
	var fileObj = $(_this)[0].files[0];
	var formFile = new FormData();
	formFile.append("file", fileObj);
	formFile.append("shopId", $("#shopId").val());
	
	var data = formFile;
	var loading = layer.load(1, {
		  shade: [0.1,'#fff'] //0.1透明度的白色背景
		});
	$.ajax({
		url: "shop/uploadFile.do",
		data: data,
		type: "post",
		dataType: "json",
		cache: false,
		processData: false,//用于对data参数进行序列化处理，必须为false
		contentType: false,//必须
		success:function(res){
			$(_this).prev().attr({name: $(_this).attr("id"), value: res.data});
			$(_this).attr("name","aaaa");
			layer.close(loading);
		}
	});
}